package com.lizonghui.googleplay.control;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lizonghui.googleplay.R;
import com.lizonghui.googleplay.base.BaseHolder;
import com.lizonghui.googleplay.bean.ItemInfoBean;
import com.lizonghui.googleplay.config.Constants;
import com.lizonghui.googleplay.utils.UIUtils;
import com.lizonghui.googleplay.views.RatioLayout;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by Administrator on 2016/6/25.
 * 模块化视图设计
 * 数据类型就仍是DetailInfoBean
 */
public class DetailPicHolder extends BaseHolder<ItemInfoBean> {

    @Bind(R.id.app_detail_pic_iv_container)
    LinearLayout mAppDetailPicIvContainer;


    //绑定数据data+view
    @Override
    public void refreshHolderView(ItemInfoBean data) {
        //图片靠的太紧了，我们设置边距。通过LinearLayout.LayoutParams
        List<String> screens = data.screen;
        for (int i = 0; i < screens.size(); i++) {
            String picUrl = screens.get(i);
            ImageView ivPic = new ImageView(UIUtils.getContext());
            Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMABASEURL + picUrl).into(ivPic);

            //发现高分辨率手机一下子展示多张图片，我们设置值展示3张，就是宽度为屏幕的1/3，去动态算出高度
            RatioLayout rl = new RatioLayout(UIUtils.getContext());
            rl.setPicRatio((float) 150 / 250);//设置图片宽高比
            rl.setRelative(RatioLayout.RELATIVE_WIDTH);//设置计算方式
            rl.addView(ivPic);//加图片添加到RationLayout中来

            //宽度为屏幕1/3
            int screenWidth  = UIUtils.getResources().getDisplayMetrics().widthPixels;//得到屏幕宽度
            screenWidth = screenWidth -UIUtils.dip2px(12);//要减去我们设置的总共的间距和默认第一张图片的间距
            int pic_width = screenWidth / 3;
            int height = LinearLayout.LayoutParams.WRAP_CONTENT;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pic_width, height);

            //第一张图片不用设置左边间距,因为在XML中整个父布局定义过了
            if (i != 0) {
                params.leftMargin = UIUtils.dip2px(4);//设置左边距
            }
            mAppDetailPicIvContainer.addView(rl, params);
        }
    }

    //初始化视图,找到孩子
    @Override
    public View initHolderView() {
        View rootView = View.inflate(UIUtils.getContext(), R.layout.item_detail_pic, null);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

}
